package the.offer.CodingInterviewGuide.Charpter_1_Stack_and_Queue;


import java.util.Stack;

public class GetMinStack {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    public GetMinStack() {
        this.stackData = new Stack<Integer>();
        this.stackMin = new Stack<Integer>();
    }

    public void push(int newNum) {
        stackData.push(newNum);
        if (stackMin.isEmpty())
            stackMin.push(newNum);
        else if (newNum < stackMin.peek())
            stackMin.push(newNum);
        else {
            Integer newMin = stackMin.peek();
            stackMin.push(newMin);
        }
    }

    public int pop() {
        if (stackData.isEmpty())
            throw new RuntimeException("Stack is empty.");
        stackMin.pop();
        return stackData.pop();
    }

    public int getMin() {
        if (stackMin.isEmpty())
            throw new RuntimeException("Stack is empty.");
        return stackMin.peek();
    }
}

